/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VCD.java
 *
 * Created on Jan 17, 2012, 9:54:07 PM
 */

package tugasbesarjeni1.form;

import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import tugasbesarjeni1.Database.VCD.Vcd;
import tugasbesarjeni1.Database.query;
import tugasbesarjeni1.form.model.VCDModel;

/**
 *
 * @author Indy
 */
public class VCD extends javax.swing.JPanel {
    private VCDModel vCDModel;
    private query Query = new query();

    /** Creates new form VCD */
    public VCD() {
        initComponents();
        initForm();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelVcd = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        tKode = new javax.swing.JTextField();
        tJudul = new javax.swing.JTextField();
        cboKategori = new javax.swing.JComboBox();
        tHargaSewa = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        bBaru = new javax.swing.JButton();
        bSimpan = new javax.swing.JButton();
        bUbah = new javax.swing.JButton();
        bHapus = new javax.swing.JButton();
        bSelesai = new javax.swing.JButton();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "VCD", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Rockwell", 1, 24))); // NOI18N
        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel2.setLayout(new java.awt.BorderLayout());

        tabelVcd.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tabelVcd.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelVcdMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabelVcd);

        jPanel2.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel1.add(jPanel2, java.awt.BorderLayout.CENTER);

        jLabel1.setText("Kode");

        jLabel2.setText("Judul");

        jLabel3.setText("Kategori");

        jLabel5.setText("Harga Sewa");

        cboKategori.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Film", "MP3", "Software" }));
        cboKategori.setSelectedIndex(-1);

        jLabel6.setText("Rp");

        bBaru.setText("Baru");
        bBaru.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bBaruActionPerformed(evt);
            }
        });

        bSimpan.setText("Simpan");
        bSimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bSimpanActionPerformed(evt);
            }
        });

        bUbah.setText("Ubah");

        bHapus.setText("Hapus");
        bHapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bHapusActionPerformed(evt);
            }
        });

        bSelesai.setText("Selesai");
        bSelesai.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bSelesaiActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tKode, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(bBaru)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bSimpan)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bUbah)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bHapus)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bSelesai))
                    .addComponent(tJudul, javax.swing.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE)
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(tHargaSewa, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(cboKategori, javax.swing.GroupLayout.Alignment.LEADING, 0, 137, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tKode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tJudul, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cboKategori, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tHargaSewa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bBaru)
                    .addComponent(bSimpan)
                    .addComponent(bUbah)
                    .addComponent(bHapus)
                    .addComponent(bSelesai))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(30, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jPanel1.add(jPanel3, java.awt.BorderLayout.PAGE_START);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 488, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 488, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 325, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 325, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void bBaruActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bBaruActionPerformed
        standBy(false);
        tKode.setEditable(true);
        tKode.requestFocus();
        bUbah.setEnabled(false);
        bHapus.setEnabled(false);
    }//GEN-LAST:event_bBaruActionPerformed

    private void bSimpanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bSimpanActionPerformed
        if (datasalah() == true) {
            simpan();
        }
    }//GEN-LAST:event_bSimpanActionPerformed

    private void bHapusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bHapusActionPerformed
        hapus();
    }//GEN-LAST:event_bHapusActionPerformed

    private void bSelesaiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bSelesaiActionPerformed
        standBy(true);
        clearForm();
    }//GEN-LAST:event_bSelesaiActionPerformed

    private void tabelVcdMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelVcdMouseClicked
        standBy(false);
        posisitable();
    }//GEN-LAST:event_tabelVcdMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bBaru;
    private javax.swing.JButton bHapus;
    private javax.swing.JButton bSelesai;
    private javax.swing.JButton bSimpan;
    private javax.swing.JButton bUbah;
    private javax.swing.JComboBox cboKategori;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField tHargaSewa;
    private javax.swing.JTextField tJudul;
    private javax.swing.JTextField tKode;
    private javax.swing.JTable tabelVcd;
    // End of variables declaration//GEN-END:variables

    private void initForm() {
       vCDModel = new VCDModel();
       tabelVcd.setModel(vCDModel);
       isitable();
       standBy(true);
    }
    private void standBy(boolean b) {
        tKode.setEnabled(!b);
        tJudul.setEnabled(!b);
        tHargaSewa.setEnabled(!b);
        cboKategori.setEnabled(!b);
        bSimpan.setEnabled(!b);
        bHapus.setEnabled(!b);
        bUbah.setEnabled(!b);
        bSelesai.setEnabled(!b);
        bBaru.setEnabled(b);
    }
    private void isitable() {
         try {
            clearTable();
            ResultSet resultSet = Query.selectVcd();
            while(resultSet.next()){
                Vcd v = new Vcd();
                v.setKode(resultSet.getString(1));
                v.setJudul(resultSet.getString(2));
                v.setKategori(resultSet.getString(3));
                v.setHargaSewa(resultSet.getString(4));
                vCDModel.tambahVcd(v);
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex, "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    private void posisitable() {
        standBy(false);
        tKode.setEditable(false);
        bSimpan.setEnabled(false);
        tabelVcd.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                int baris = tabelVcd.getSelectedRow();
                if (baris != -1) {
                    Vcd vcd = vCDModel.get(baris);
                    String kategori = vcd.getKategori();
                    if(kategori.equals("Film")){
                        cboKategori.setSelectedIndex(0);                        
                    }else if(kategori.equals("MP3")){
                        cboKategori.setSelectedIndex(1);
                    }else if(kategori.equals("Software")){
                        cboKategori.setSelectedIndex(2);
                    }
                    tKode.setText(vcd.getKode());
                    tJudul.setText(vcd.getJudul());
                    tHargaSewa.setText(vcd.getHargaSewa());
                }
            }
        });
    }
    private boolean datasalah() {
        boolean bol;
        if ((tKode.getText().equals(""))|| (tJudul.getText().equals(""))|| (tHargaSewa.getText().equals(""))|| (cboKategori.getSelectedIndex()==-1)) {
            bol = false;
            JOptionPane.showMessageDialog(null, "Data tidak lengkap", "Perhatian", JOptionPane.WARNING_MESSAGE);
        } else {
            bol = true;
        }
        return bol;
    }
    private void simpan() {
        Query.simpanVcd(tKode.getText(), tJudul.getText(), cboKategori.getSelectedItem().toString(), tHargaSewa.getText());
        isitable();
        clearForm();
    }
    private void clearTable() {
       int row = tabelVcd.getRowCount();
        for (int i = 0; i < row; i++) {
            vCDModel.hapusTable(0, row);
        }
    }
    private void hapus() {
        int ok;
        if ((ok = JOptionPane.showConfirmDialog(null, "Hapus data " + tJudul.getText() + " ?", "Hapus", JOptionPane.YES_NO_OPTION)) == 0) {
            Query.hapusVcd(tKode.getText());
            isitable();
            standBy(true);
            clearForm();
        }
    }
    private void ubah() {
        int ok;
        String jenis = null;
        if ((ok = JOptionPane.showConfirmDialog(null, "Ubah data " + tJudul.getText() + " ?", "Ubah", JOptionPane.YES_NO_OPTION)) == 0) {

            Query.updateVcd(tKode.getText(), tJudul.getText(), cboKategori.getSelectedItem().toString(), tHargaSewa.getText());
            isitable();
            standBy(true);
            clearForm();
        }
    }
    private void clearForm() {
        tKode.requestFocus();
        tKode.setText("");
        tJudul.setText("");
        tHargaSewa.setText("");
        cboKategori.setSelectedIndex(-1);
    }
}
